package com.example.wyh.intelcheckbash.bug;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.wyh.intelcheckbash.R;
import com.example.wyh.intelcheckbash.product_visit.ProductVisitListActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by chen on 17/12/6.
 */

public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {
    private Context mContext;
    private JSONArray mList;
    //普通布局的type
    static final int TYPE_ITEM = 0;
    //脚布局
    static final int TYPE_FOOTER = 1;
    //    //上拉加载更多
//    static final int PULL_LOAD_MORE = 0;
    //正在加载更多
    static final int LOADING_MORE = 1;
    //没有更多
    static final int NO_MORE = 2;
    //脚布局当前的状态,默认为没有更多
    int footer_state = 0;

    public String mTYPE = "";

    public MyRecyclerAdapter(Context context, JSONArray list, String type) {
        mContext = context;
        mList = list;
        mTYPE = type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = View.inflate(mContext, R.layout.bug_item, null);
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        } else if (viewType == TYPE_FOOTER) {
            //脚布局
            View view = View.inflate(mContext, R.layout.recycler_load_more_layout, null);
            FootViewHolder footViewHolder = new FootViewHolder(view);
            return footViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyViewHolder) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mTYPE.equals("BugListRecordActivity"))
                    {
                        BugListRecordActivity bugListRecordActivity = (BugListRecordActivity)mContext;
                        bugListRecordActivity.jumpToBugInfo(position);
                    }
                    else if (mTYPE.equals("ProductVisitListActivity"))
                    {
                        ProductVisitListActivity productVisitListActivity = (ProductVisitListActivity)mContext;
                        productVisitListActivity.jumpToProductInfo(position);
                    }

                }
            });
            try {
                JSONObject equipment = mList.getJSONObject(position);
                if (equipment.getString("isUpload").equals("0"))
                {
                    ((MyViewHolder) holder).upload_bug_status.setText("未上传");
                    ((MyViewHolder) holder).upload_bug_status.setTextColor(Color.parseColor("#ffc71e"));
                    ((MyViewHolder) holder).left_line_show.setBackgroundResource(R.drawable.left_yellow_line);
                }
                else
                {
                    ((MyViewHolder) holder).left_line_show.setBackgroundResource(R.drawable.left_blue_line);
                    ((MyViewHolder) holder).upload_bug_status.setText("已上传");
                    ((MyViewHolder) holder).upload_bug_status.setTextColor(Color.parseColor("#41bdff"));
                }
                if (mTYPE.equals("BugListRecordActivity"))
                {
                    ((MyViewHolder) holder).bug_name_item_text.setText(equipment.getString("qxmc"));
                    ((MyViewHolder) holder).bug_level_item_text.setText(equipment.getString("qxdj"));
                    ((MyViewHolder) holder).bug_equipment_name_item_text.setText(equipment.getString("sbmc"));
                }
                else if (mTYPE.equals("ProductVisitListActivity"))
                {
                    ((MyViewHolder) holder).bug_name_item_text.setText(equipment.getString("xsqy"));
                    ((MyViewHolder) holder).bug_level_item_text.setText(equipment.getString("xsqysj"));
                    ((MyViewHolder) holder).bug_equipment_name_item_text.setText(equipment.getString("xsjg"));
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            if (position < 20) {//如果第一个就是脚布局,,那就让他隐藏
                footViewHolder.mProgressBar.setVisibility(View.GONE);
                footViewHolder.tv_line1.setVisibility(View.GONE);
                footViewHolder.tv_line2.setVisibility(View.GONE);
                footViewHolder.tv_state.setText("");
            }
            switch (footer_state) {//根据状态来让脚布局发生改变
                case LOADING_MORE:
                    footViewHolder.mProgressBar.setVisibility(View.VISIBLE);
                    footViewHolder.tv_line1.setVisibility(View.GONE);
                    footViewHolder.tv_line2.setVisibility(View.GONE);
                    footViewHolder.tv_state.setText("正在加载...");
                    break;
                case NO_MORE:
                    footViewHolder.mProgressBar.setVisibility(View.GONE);
                    footViewHolder.tv_line1.setVisibility(View.VISIBLE);
                    footViewHolder.tv_line2.setVisibility(View.VISIBLE);
                    footViewHolder.tv_state.setText("已经滑倒最底端了");
                    footViewHolder.tv_state.setTextColor(Color.parseColor("#41bdff"));
                    break;
            }
        }
    }


    @Override
    public int getItemViewType(int position) {
        //如果position加1正好等于所有item的总和,说明是最后一个item,将它设置为脚布局
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.length() + 1 : 0;
    }

    /**
     * 正常布局的ViewHolder
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView bug_name_item_text;
        TextView bug_level_item_text;
        TextView bug_equipment_name_item_text;
        TextView upload_bug_status;
        LinearLayout left_line_show;
        public MyViewHolder(View itemView) {
            super(itemView);
            bug_name_item_text = (TextView) itemView.findViewById(R.id.bug_name_item_text);
            bug_level_item_text = (TextView) itemView.findViewById(R.id.bug_level_item_text);
            bug_equipment_name_item_text = (TextView) itemView.findViewById(R.id.bug_equipment_name_item_text);
            upload_bug_status = (TextView)itemView.findViewById(R.id.upload_bug_status);
            left_line_show = (LinearLayout)itemView.findViewById(R.id.left_line_show);
        }
    }

    /**
     * 脚布局的ViewHolder
     */
    public static class FootViewHolder extends RecyclerView.ViewHolder {
        private ProgressBar mProgressBar;
        private TextView tv_state;
        private TextView tv_line1;
        private TextView tv_line2;


        public FootViewHolder(View itemView) {
            super(itemView);
            mProgressBar = (ProgressBar) itemView.findViewById(R.id.progressbar);
            tv_state = (TextView) itemView.findViewById(R.id.foot_view_item_tv);
            tv_line1 = (TextView) itemView.findViewById(R.id.tv_line1);
            tv_line2 = (TextView) itemView.findViewById(R.id.tv_line2);

        }
    }

    /**
     * 改变脚布局的状态的方法,在activity根据请求数据的状态来改变这个状态
     *
     * @param state
     */
    public void changeState(int state) {
        this.footer_state = state;
        notifyDataSetChanged();
    }
}
